from rest_framework import serializers
from .models import City, Datacenter, Host, PasswordHistory, HostStatistics


class CitySerializer(serializers.ModelSerializer):
    class Meta:
        model = City
        fields = '__all__'


class DatacenterSerializer(serializers.ModelSerializer):
    city_name = serializers.ReadOnlyField(source='city.name')

    class Meta:
        model = Datacenter
        fields = '__all__'


class HostSerializer(serializers.ModelSerializer):
    datacenter_name = serializers.ReadOnlyField(source='datacenter.name')
    city_name = serializers.ReadOnlyField(source='datacenter.city.name')

    class Meta:
        model = Host
        fields = '__all__'
        extra_kwargs = {
            'root_password': {'write_only': True}  # 保护密码不被返回
        }


class PasswordHistorySerializer(serializers.ModelSerializer):
    host_name = serializers.ReadOnlyField(source='host.hostname')

    class Meta:
        model = PasswordHistory
        fields = ['id', 'host', 'host_name', 'changed_at', 'changed_by']
        # 不返回密码信息


class HostStatisticsSerializer(serializers.ModelSerializer):
    city_name = serializers.ReadOnlyField(source='city.name')
    datacenter_name = serializers.ReadOnlyField(source='datacenter.name')

    class Meta:
        model = HostStatistics
        fields = '__all__'